class BaseEmitter extends Trigger;

var float total;
var float counter;
var float step;
var Rotator rot;
var AudioComponent AC_Rotation;
var LazerBeam Lazer;
var StaticMeshComponent Mesh;

var bool IsInInteractionRange;

var enum EmitterDirection
{
	D0,
	D90,
	D180,
	D270
} Direction;

function float GetRotationValue(EmitterDirection dir)
{
	return 16383.75;
}

function ToggleEmitter()
{
	local Vector trHitNormal, trHitLocation, end, pos;
	local Actor tracedActor;
	local TraceHitInfo hitInfo;

	pos = Location;
	pos.Z += 120;

	if(Lazer == None)
	{
		Lazer = Spawn(class'LazerBeam',,,pos);
	}

	if(!Lazer.IsActive())
	{
		end = normal(vector(Rotation))*32768; // trace to "infinity"
		tracedActor = Trace(trHitLocation, trHitNormal, end, pos, true,, hitInfo);

		if(tracedActor != none)
		{
			Lazer.SetupBeam(trHitLocation, Rotation);
		}
	}

	Lazer.Toggle();
}

event Touch(Actor Other, PrimitiveComponent OtherComp, Vector HitLocation, Vector HitNormal)
{
	super.Touch(Other, OtherComp, HitLocation, HitNormal);
	if (Pawn(Other) != none)
	{
		IsInInteractionRange = true;
		AzorunPlayerController(Pawn(Other).Controller).bIsReadyToInteraction = true;
		AzorunPlayerController(Pawn(Other).Controller).BeamEmitter = self;
	}
}

event UnTouch(Actor Other)
{
	super.UnTouch(Other);
	if (Pawn(Other) != none)
	{
		IsInInteractionRange = false;
		AzorunPlayerController(Pawn(Other).Controller).bIsReadyToInteraction = false;
		AzorunPlayerController(Pawn(Other).Controller).BeamEmitter = None;
	}
}

auto state NormalState
{
	function bool UsedBy(Pawn User)
	{
		local bool used;
		used = super.UsedBy(User);

		if (IsInInteractionRange)
		{
			GotoState('RotationState');
			return true;
		}
		return used;
	}
}

state RotationState
{
	ignores UsedBy;

	function EmitterDirection GetNextState()
	{
		if(Direction == EmitterDirection.D0)
			return EmitterDirection.D90;
		if(Direction == EmitterDirection.D90)
			return EmitterDirection.D180;
		if(Direction == EmitterDirection.D180)
			return EmitterDirection.D270;

		return EmitterDirection.D0;
	}

	Begin:
		counter = 0.0;
		step = 500.0;
		rot = Rotation;
		total = GetRotationValue(Direction);
		Direction = GetNextState();
		AC_Rotation.FadeIn(0.5f, 1); // or AC_Alarm.Play()

		while(counter < total)
		{
			counter += step;
			rot.Yaw += step;
			SetRotation(rot);
			Sleep( 0.03 );
		}
		//fix position
		if(rot.Yaw >= 65535)
		{
			rot.Yaw = 0;
			SetRotation(rot);
		}
		
		AC_Rotation.FadeOut(0.5, 0); // or AC_Alarm.Stop()

		PlaySound(SoundCue'A_Character_Footsteps.FootSteps.A_Character_Footstep_MetalCue');
		GotoState('NormalState');
}

DefaultProperties
{
	Begin Object Class=DynamicLightEnvironmentComponent Name=MyLightEnvironment
		bSynthesizeSHLight=TRUE
		bIsCharacterLightEnvironment=TRUE
		bUseBooleanEnvironmentShadowing=FALSE
	End Object
	Components.Add(MyLightEnvironment)

	begin object Name=Sprite
		HiddenGame=true
	HiddenEditor=true
	end object

	begin object class=StaticMeshComponent Name=MyMesh
		StaticMesh=StaticMesh'Azorun_tutormap.laser'
		LightEnvironment = MyLightEnvironment
		//Rotation=(Yaw=32768)
	end object

	Begin Object Name=CollisionCylinder
		CollisionRadius=+0080.000000
		CollisionHeight=+0044.000000
	End Object
	CylinderComponent=CollisionCylinder

	Begin Object class=AudioComponent Name=RotationComponentSound
		SoundCue = SoundCue'A_Vehicle_Manta_UT3g.SoundCues.A_Vehicle_Manta_EngineLoop'
	End Object

	AC_Rotation = RotationComponentSound
	Components.Add(RotationComponentSound)

	CollisionComponent=MyMesh
	Components.Add(MyMesh)
	Mesh=MyMesh

	bBlockActors=true
	bHidden=false
}
